{include file="Layout/header"/}
<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-sm12 layui-col-md12 layui-col-lg12">
      <div class="layui-card">
        <div class="layui-card-body">
          <table class="layui-table" id="menuTable" lay-filter="menuTable"></table>
        </div>
      </div>
    </div>
  </div>
</div>
<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    {php} if(in_array("admin/sysmenu/add",session('menunames'))){ {/php}
    <button class="layui-btn layui-btn-sm layui-btn-normal layui-btn-radius" lay-event="add"><i class="layui-icon">&#xe654;</i>新增</button>
    {php} } {/php}
    {php} if(in_array("admin/sysmenu/edit",session('menunames'))){ {/php}
    <button class="layui-btn layui-btn-sm layui-btn-normal layui-btn-radius" lay-event="edit"><i class="layui-icon">&#xe642;</i>修改</button>
    {php} } {/php}
    {php} if(in_array("admin/sysmenu/delete",session('menunames'))){ {/php}
    <button class="layui-btn layui-btn-sm layui-btn-danger layui-btn-radius" lay-event="delete"><i class="layui-icon">&#xe640;</i>删除</button>
    {php} } {/php}
    <button class="layui-btn layui-btn-sm layui-btn-radius" lay-event="expandAll"><i class="layui-icon">&#xe654;</i>展开所有</button>
    <button class="layui-btn layui-btn-sm layui-btn-radius" lay-event="foldAll"><i class="layui-icon">&#xe656;</i>折叠所有</button>
  </div>
</script>
{include file="Layout/commonjs"/}
<script>
    layui.use(['layer', 'form', 'table', 'formSelects', 'dtree', 'treetable'], function () {
        var renderTable = function () {
            var loadingindex = layer.msg('数据加载中...');
            layui.treetable.render({
                treeColIndex: 3,          // treetable新增参数
                treeSpid: 0,             // treetable新增参数
                treeIdName: 'id',       // treetable新增参数
                treePidName: 'pid',     // treetable新增参数
                treeDefaultClose: false,   // treetable新增参数
                treeLinkage: true,        // treetable新增参数
                elem: '#menuTable',
                url: '{:url("/admin/sysmenu/view")}',
                method: "post",
                page: false,
                //skin: 'line',
                //even: true, //开启隔行背景
                size: 'sm', //小尺寸的表格
                limit: '20',
                toolbar: "#toolbarDemo",
                defaultToolbar: ['filter', 'exports', 'print'],
                //defaultToolbar: [],
                loading: true,
                height: 'full-100',
                cellMinWidth: 100,
                cols: [[
                    {type: 'radio'},
                    {field: 'id', title: 'ID', width: 80},
                    {field: 'pid', title: '父节点ID', width: 100},
                    {field: 'title', title: '名称', width: 250},
                    {field: 'name', title: '权限规则'},
                    {
                        field: 'status', width: 80, align: 'center', title: '状态', templet: function (d) {
                            if (d.status == 1) {
                                return '<span class="layui-badge layui-bg-blue">启用</span>';
                            } else {
                                return '<span class="layui-badge-rim">禁用</span>';
                            }
                        }
                    },
                    {field: 'icon', title: '图标', width: 150},
                    {
                        field: 'ismenu', width: 80, align: 'center', title: '类型', templet: function (d) {
                            if (d.ismenu == 1) {
                                return '<span class="layui-badge layui-bg-blue">菜单</span>';
                            } else {
                                return '<span class="layui-badge-rim">节点</span>';
                            }
                        }
                    },
                    {field: 'sort', title: '排序', width: 80},
                    {field: 'condition', title: '附件条件'},
                    {field: "", title: '操作', toolbar: "#operation", width: 100, fixed: 'right', align: 'center', hide: true},
                ]],
                done: function () {
                    layui.layer.close(loadingindex);
                }
            });
        }
        renderTable();
        layui.table.on('toolbar(menuTable)', function (obj) {
            //console.log(obj);
            var data = layui.table.checkStatus(obj.config.id).data[0];
            //console.log(data);
            if (obj.event == 'add') {
                $.get('{:url("/admin/sysmenu/add")}', {}, function (str) {
                    var index = layui.layer.open({
                        title: "修改",
                        offset: 't',
                        area: ['530px', '500px'],
                        shadeClose: true,
                        type: 1,
                        anim: 2,
                        resize: false,
                        btn: ['保存', '取消'],
                        content: str,
                        yes: function (index, layero) {
                            //return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                            var data = $("#sysmenuForm").serializeObject();
                            var param = dtree.getNowParam("parentIdTree");
                            console.log(param.nodeId);
                            data.pid = param.nodeId;
                            $.ajax({
                                async: true,//默认true为异步
                                url: '{:url("/admin/sysmenu/add")}',
                                type: "post",
                                data: data,
                                dataType: "json",
                                beforeSend: function (xhr) {
                                },
                                success: function (res, status, xhr) {
                                    if (res.code == 1) {
                                        renderTable();
                                        layui.layer.close(index);
                                    } else {
                                        layer.msg(res.msg);
                                    }
                                },
                                error: function (xhr, status, error) {
                                }
                            });
                        },
                        success: function (layero, index) {
                        },
                        btn2: function (index, layero) {
                            //按钮【按钮二】的回调
                            //return false 开启该代码可禁止点击该按钮关闭
                        },
                        btn3: function (index, layero) {
                            //按钮【按钮三】的回调
                            //return false 开启该代码可禁止点击该按钮关闭
                        },
                        cancel: function () {
                            //右上角关闭回调
                            //return false 开启该代码可禁止点击该按钮关闭
                        }
                    })
                });
            }
            if (obj.event == 'delete') {
                if (data != null) {
                    layui.layer.confirm('确定删除此信息？', {icon: 2, title: '提示信息'}, function (index) {
                        $.ajax({
                            type: "post",
                            url: '{:url("/admin/sysmenu/delete")}',
                            data: {'id': data.id},
                            dataType: 'json',
                            success: function (d) {
                                layui.layer.msg("删除成功");
                                renderTable();
                            }
                        })
                        layer.close(index);
                    });
                } else {
                    layer.msg('请勾选记录！');
                }
            }
            if (obj.event == 'edit') {
                if (data != null) {
                    $.get('{:url("/admin/sysmenu/edit")}', {"id": data.id}, function (str) {
                        var index = layui.layer.open({
                            title: "修改",
                            offset: 't',
                            area: ['530px', '500px'],
                            shadeClose: true,
                            type: 1,
                            anim: 2,
                            resize: false,
                            btn: ['保存', '取消'],
                            content: str,
                            yes: function (index, layero) {
                                //return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                                var data = $("#sysmenuForm").serializeObject();
                                var param = layui.dtree.getNowParam("parentIdTree");
                                data.pid = param.nodeId;
                                $.ajax({
                                    async: true,//默认true为异步
                                    url: '{:url("/admin/sysmenu/edit")}',
                                    type: "post",
                                    data: data,
                                    dataType: "json",
                                    beforeSend: function (xhr) {
                                    },
                                    success: function (res, status, xhr) {
                                        if (res.code == 1) {
                                            renderTable();
                                            layui.layer.close(index);
                                        } else {
                                            layer.msg('失败！请重试！');
                                        }
                                    },
                                    error: function (xhr, status, error) {
                                    }
                                });
                            },
                            success: function (layero, index) {
                            },
                            btn2: function (index, layero) {
                                //按钮【按钮二】的回调
                                //return false 开启该代码可禁止点击该按钮关闭
                            },
                            btn3: function (index, layero) {
                                //按钮【按钮三】的回调
                                //return false 开启该代码可禁止点击该按钮关闭
                            },
                            cancel: function () {
                                //右上角关闭回调
                                //return false 开启该代码可禁止点击该按钮关闭
                            }
                        })
                    });
                } else {
                    layer.msg('请勾选记录！');
                }
            }
            if (obj.event == 'expandAll') {
                layui.treetable.expandAll('#menuTable');
            }
            if (obj.event == 'foldAll') {
                layui.treetable.foldAll('#menuTable');
            }
        });
        layui.table.on('tool(menuTable)', function (obj) {
            console.log(obj);
            //var data = layui.table.checkStatus(obj.config.id);
            //console.log(data);
            //var data = obj.data;
            if (obj.event == 'off') {
                self.location.href = 'troubleclose.html?id=' + data.id;
            }
        });
    });
</script>
</body>
</html>
